package com.superman.supermarket.client.view.orderdetail;

import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;
import com.superman.mvpframe.client.mvp.IPageViewPresenter;
import com.superman.mvpframe.client.utils.XJsonUtil;
import com.superman.supermarket.client.composite.orderdetail.OrderDetailAddrModule;
import com.superman.supermarket.client.composite.orderdetail.OrderDetailInfoModule;
import com.superman.supermarket.client.composite.orderlist.OrderListStoreModule;
import com.superman.supermarket.client.composite.orderlist.opt.OrderListOpButton;
import com.superman.supermarket.client.constant.OrderStatusConstants;

/**
 * 订单详情ui实现类
 * 
 * @author superman
 * @version 2018年8月2日下午5:25:57
 */
public class OrderDetailViewImpl extends Composite implements OrderDetailView {

	interface OrderDetailViewImplUiBinder extends UiBinder<Widget, OrderDetailViewImpl> {
	}

	private static OrderDetailViewImplUiBinder uiBinder = GWT.create(OrderDetailViewImplUiBinder.class);

	@UiField
	OrderDetailAddrModule addrmodule;
	@UiField
	OrderDetailInfoModule orderinfomodule;
	@UiField
	FlowPanel orderotherpanel, bottompanel;
	@UiField
	HTML orderstatushtml;
	@UiField
	FlowPanel storelistpanel;

	/**
	 * 构造方法
	 * 
	 * @author superman
	 * @version 2018年8月12日 下午10:15:14
	 */
	public OrderDetailViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));

		orderinfomodule.getElement().getStyle().setProperty("marginBottom", "0px");
	}

	private void addOtherInfoLine(String name, String value) {
		HTML lineHTML = new HTML();
		lineHTML.setHTML(name + ":&nbsp;&nbsp;&nbsp;&nbsp;" + value);
		orderotherpanel.add(lineHTML);
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub

	}

	@Override
	public void setAddrInfo(Map<String, Object> map) {
		addrmodule.setData(map);
	}

	@Override
	public void setOrderInfoModule(Map<String, Object> map) {
		orderotherpanel.clear();

		int status = (int) Double.parseDouble(String.valueOf(map.get("ORDER_STATE")));
		orderstatushtml.setText(OrderStatusConstants.getDescriptionByStatus(status));

		orderinfomodule.setData(map);
		addOtherInfoLine("订单编号", String.valueOf(map.get("ORDER_ID")));
		addOtherInfoLine("订单创建时间", String.valueOf(map.get("CREATE_TIME")));

		if (map.get("PAYMENT_TIME") != null)
			addOtherInfoLine("订单付款时间", String.valueOf(map.get("PAYMENT_TIME")));

		// 可操作的按钮
		if (map.get("OP") == null || XJsonUtil.getOnlyList((JSONArray) map.get("OP")).size() == 0)
			bottompanel.removeFromParent();
		else {
			bottompanel.clear();
			List<Object> ops = XJsonUtil.getOnlyList((JSONArray) map.get("OP"));
			for (Object object : ops) {
				int op = (int) Double.parseDouble(String.valueOf(object));
				OrderListOpButton orderListOpButton = new OrderListOpButton(map, op);
				bottompanel.add(orderListOpButton);
			}
		}
	}

	@Override
	public void setPageViewPresenter(IPageViewPresenter presenter) {
		// TODO Auto-generated method stub

	}

	@Override
	public void setStoreSkuList(List<Map<String, Object>> list, String orderId) {
		storelistpanel.clear();
		for (Map<String, Object> map : list) {
			map.put("ORDER_ID", orderId);
			OrderListStoreModule orderListStoreModule = new OrderListStoreModule();
			orderListStoreModule.setData(map);
			storelistpanel.add(orderListStoreModule);
		}
	}

}
